import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:permission_handler/permission_handler.dart';

class WebViewPage2 extends StatefulWidget {
  final String title;
  final String url;

  const WebViewPage2({super.key, required this.title, required this.url});

  @override
  _MyWebViewState createState() => _MyWebViewState();
}

Future<void> _requestPermissions() async {
  await Permission.camera.request();
  await Permission.photos.request();
  await Permission.storage.request();
}

class _MyWebViewState extends State<WebViewPage2> {
  late InAppWebViewController webViewController;
  @override
  void initState() {
    super.initState();
    _requestPermissions();
  }
  Future<bool> _exitApp(BuildContext context) async {
    if (await webViewController.canGoBack()) {
      print("onwill goback");
      webViewController.goBack();
      return Future.value(false);
    } else {
      debugPrint("_exit will not go back");
      return Future.value(true);
    }
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () => _exitApp(context),
      child: Scaffold(
          body:Padding(padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top),
            child: InAppWebView(
              initialUrlRequest: URLRequest(url: WebUri(widget.url)),
              initialSettings: InAppWebViewSettings(
                useShouldInterceptAjaxRequest: true,
                  mediaPlaybackRequiresUserGesture: false
              ),
              onWebViewCreated: (controller) {
                webViewController = controller;
              },
              onLoadStart: (controller, url) {
                print("Loading: $url");
              },
              onLoadStop: (controller, url) async {
                print("Loaded: $url");
              },
              onConsoleMessage: (controller, consoleMessage) {
                print(consoleMessage.message);
              },
              onPermissionRequest: (controller, request) async {
                return PermissionResponse(
                    resources: request.resources,
                    action: PermissionResponseAction.GRANT);
              },
              shouldOverrideUrlLoading: (controller, navigationAction) async {
                print("vvvvvvv");
                // 拦截文件选择请求
                if (navigationAction.isForMainFrame &&
                    navigationAction.request.url.toString().startsWith('intent://')) {
                  // 使用 InAppWebView.openFileChooser 打开文件选择器
                  // await webViewController.openFileChooser(
                  //   controller,
                  //   // 根据实际情况设置参数
                  //   multipleFiles: false,
                  //   acceptTypes: ['image/*'],
                  // );
                  return NavigationActionPolicy.CANCEL;
                }
                return NavigationActionPolicy.ALLOW;
              },
            )),
          )
    );
  }
}
